package com.wjk.kylin.mall.common.web.util;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;

import java.beans.PropertyEditorSupport;

/**
 * @author admin
 * @description String进行HTML编码，防止XSS攻击
 * @date 2019/7/3
 */
@Slf4j
public class StringEditor extends PropertyEditorSupport {

    @Override
    public void setAsText(String text) {
        log.info("StringEditor setAsText before text:{}", text);
        String html = text == null ? null : StringEscapeUtils.escapeHtml4(text.trim());
        log.info("StringEditor setAsText after text:{}", html);
        setValue(html);
    }

    @Override
    public String getAsText() {
        Object value = getValue();
        return value != null ? value.toString() : "";
    }
}
